/**
 * @author : xwj
 * @date : 2021/7/21 20:59
 * @description : 编写一个函数来查找字符串数组中的最长公共前缀。
 *                如果不存在公共前缀，返回空字符串 ""。
 * @class : test14
 **/

public class test14 {

    public static String longestCommonPrefix(String[] strs) {
        if (strs.length == 0){
            return "";
        }
        String s = strs[0];
        for (String str: strs) {
            boolean flag = true;
            while (flag){
                if (str.startsWith(s)){
                    flag = false;
                }else {
                    s = s.substring(0,s.length()-1);
                    if (s.equals("")){
                        return "";
                    }
                }
            }
        }
        return s;
    }

    public static void main(String[] args) {
        String[] strs = {"dog","racecar","car"};
        System.out.println(longestCommonPrefix(strs));
    }

}
